//
//  JEHSubscribeViewController.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/5.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHSubscribeViewController.h"

@interface JEHSubscribeViewController ()<UITableViewDataSource, UITableViewDelegate, passOtherValue>

@property (nonatomic, retain) UITableView *leftTableView;
@property (nonatomic, retain) UITableView *rightTableView;
@property (nonatomic, retain) NSArray *leftTitleArray;
@property (nonatomic, retain) NSArray *rightIntroArray;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSArray *firstRowNameArray;
@property (nonatomic, retain) NSArray *firstRowUrlArray;

@end

@implementation JEHSubscribeViewController

- (void)dealloc
{
    [_leftTableView release];
    [_rightTableView release];
    [_leftTitleArray release];
    [_rightIntroArray release];
    [_subscribeDataSource release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    _delegate = nil;
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.leftTableView nightMode];
    [self.rightTableView nightMode];
    self.tabBarController.tabBar.alpha = 0;
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self createHeaderView];
    [self createLeftTableView];
    [self createRightTableView];
}

#pragma mark - rewriteSetterMethod
- (void)setSubscribeDataSource:(NSArray *)subscribeDataSource {
    if (_subscribeDataSource != subscribeDataSource) {
        [_subscribeDataSource release];
        _subscribeDataSource = [subscribeDataSource retain];
    }
}

#pragma mark - getData
- (void)getData {
    
    self.leftTitleArray = @[@"新浪频道", @"头条", @"推荐", @"娱乐", @"体育", @"财经", @"汽车", @"搞笑", @"辽宁"];
    self.rightIntroArray = @[kHeadlineGETURL, kRecommendGETURL, kEntertainmentGETURL, kSportsGETURL, kFinanceGETURL, kAutomobilesGETURL, kTechnologyGETURL, kFunnyGETURL, kLiaoningGETURL];
    self.dataSource = [NSMutableArray arrayWithArray:self.subscribeDataSource];
}

#pragma mark - createHeaderView
- (void)createHeaderView {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kHeaderHeight + 20)];
    [self.view addSubview:headerView];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderHeight + 19, headerView.width, 1)];
    topView.backgroundColor = [UIColor grayColor];
    topView.alpha = 0.3;
    [headerView addSubview:topView];
    
    UIButton *backBotton = [UIButton buttonWithType:UIButtonTypeCustom];
    backBotton.frame = CGRectMake(0, 20, 50, kHeaderHeight);
    [backBotton setImage:[UIImage imageNamed:@"goBackButton"] forState:UIControlStateNormal];
    [backBotton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:backBotton];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.width / 2 - 20, 20, 40, kHeaderHeight)];
    title.text = @"订阅";
    [title nightMode];
    [headerView addSubview:title];
}

#pragma mark - backButtonAction
- (void)backButtonAction:(UIButton *)sender {

    JEHMoreViewController *more = [self.navigationController.viewControllers objectAtIndex:1];
    [self.navigationController popToViewController:more animated:YES];
}

#pragma mark - createTableView
- (void)createLeftTableView {
    
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeaderHeight + 20, self.view.width / 4, self.view.height - kHeaderHeight - 20) style:UITableViewStyleGrouped];
    [_leftTableView release];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    [_leftTableView registerClass:[JEHLeftTableViewCell class] forCellReuseIdentifier:@"JEHLeftTableViewCellIdentifier"];
    [self.view addSubview:_leftTableView];
}

- (void)createRightTableView {
    
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.width / 4, kHeaderHeight + 20, self.view.width * 3 / 4, self.view.height - kHeaderHeight - 20) style:UITableViewStyleGrouped];
    [_rightTableView release];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    [_rightTableView registerClass:[JEHRightTableViewCell class] forCellReuseIdentifier:@"JEHRightTableViewCellIdentifier"];
    [_leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"A"];
    [self.view addSubview:_rightTableView];
}

#pragma mark - UITableViewDelegateMethod
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if ([tableView isEqual:_leftTableView]) {
        
        return self.leftTitleArray.count;
    } else {
        if (_dataSource) {
            return self.dataSource.count;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView isEqual:_leftTableView]) {
        
        JEHLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JEHLeftTableViewCellIdentifier"];
        if (indexPath.row == 0) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.leftTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
            cell.titleLabel.text = NULL;
            cell.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:240 / 255.0 alpha:1];
            cell.selectedBackgroundView = cell.backView;
            cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
        } else {
            cell.title = self.leftTitleArray[indexPath.row];
            [cell.firstImage removeFromSuperview];
            cell.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:240 / 255.0 alpha:1];
            cell.selectedBackgroundView = cell.backView;
            cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
            cell.titleLabel.highlightedTextColor = [UIColor redColor];
        }
        return cell;
    }
    if ([tableView isEqual:_rightTableView]) {

        if (_dataSource) {
            
            JEHRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JEHRightTableViewCellIdentifier"];
            cell.rootModel = self.dataSource[indexPath.row];
            cell.temp = indexPath.row;
            [cell nightMode];
            [cell.nameLabel nightMode];
            [cell.titleLabel nightMode];
            [cell.pictureView nightMode];
            return cell;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"A"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_leftTableView]) {
        return 40;
    } else {
        return 60;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView isEqual:_leftTableView]) {
        
        if (indexPath.row > 0) {
        
            [JEHNetWorkTool getWithUrl:self.rightIntroArray[indexPath.row - 1] parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
                
                if (result) {

                    self.dataSource = [NSMutableArray arrayWithCapacity:0];
                    NSDictionary *dic = result[@"data"];
                    NSArray *array = dic[@"list"];
                    for (NSDictionary *dic in array) {
                        JEHRootModel *root = [[JEHRootModel alloc] initWithDictionary:dic];
                        [self.dataSource addObject:root];
                        [root release];
                    }
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        [self.rightTableView reloadData];
                    });
                }
            } fail:^(NSError *error) {
                NSLog(@"网不好QAQ");
            }];
        } else {
            self.dataSource = [NSMutableArray arrayWithArray:self.subscribeDataSource];
            [self.rightTableView reloadData];
        }
    } else {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

#pragma mark - passOtherValue
- (void)passOtherValue:(NSInteger)temp {
    
    [self.delegate passOtherValueToMoreVC:temp];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
